/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：
* CreateTime：2020-04-28 10:26:36
* Version：1.0
* Modify Recorder：
*************************************************/

using UnityEngine;
using UnityEditor;
using TinyToolKit;

namespace TinyToolKitEditor
{
    //用于绘制特性，该类需要放到Editor中
    [CustomPropertyDrawer(typeof(TimeFormatAttribute))]
    public class TimeFormatDrawer : PropertyDrawer
    {
        private TimeFormatAttribute Attribute => attribute as TimeFormatAttribute; 
        
        //设置绘制的区域高度
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return EditorGUI.GetPropertyHeight(property) * 2;
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType == SerializedPropertyType.Integer)
            {
                property.intValue = EditorGUI.IntField(new Rect(position.x, position.y, position.width, position.height / 2), label, Mathf.Max(0, property.intValue));

                EditorGUI.LabelField(new Rect(position.x, position.y + position.height / 2, position.width, position.height / 2), " ", TimeConvert(property.intValue));
            }
            else
            {
                EditorGUI.HelpBox(position, "使用TimeFormatAttribute的字段 '" + label.text + "' 必须是整数类型", MessageType.Error);
            }
        }

        private string TimeConvert(int value)
        {
            return TinyUtility.Time.ConvertSecondsToTime(value, Attribute.ShowHour);  
        }
    }
}
